<?php
/**
 * @Name: BaseServices.php
 * @Author: yashuai<1762910894@qq.com>
 * @Desc
 */

namespace app\common\services;

use think\db\Query;
use think\facade\Db;

/**
 * @method query($id = false) 获取模型
 * @method get($id, array $field = [], array $with = [], array $setting = []) 查询记录
 * @method count(array $where = []) 查询总数
 * @method sum(string $field, $where = []) 查询总数
 * @method value($id, string $field) 查询某一个字段sss
 * @method column($id, $field, string $key = '') 获取某一列的值
 * @method createRow(array $params, bool $turn = true) 添加记录（用于表单提交）
 * @method createRowAll(array $data, array $merge = []) 批量添加记录
 * @method update($id, array $data) 更新记录
 * @method updateRow(array $params, array $where = [], bool $turn = true) 修改记录（用于表单提交）
 * @method createOrUpdate(array $where, array $data, array $create_data = []) 添加或者更新
 * @method deleteRow(int|array $id, array $where = []) 删除记录
 * @method physicalDeleteRow(array $where = []) 物理删除
 * @method validatorRow(int|array $id, array $field = []) 验证记录是否存在
 * @method selectList(array $where, $field = '*', $page = 0, $limit = 0, array $with = [], array $order = [], array $setting = []) 列表查询
 */
abstract class BaseServices
{

    /**
     * 模型注入
     * @var object
     */
    protected $logic;

    /**
     * @Desc 分页列表
     * @param array $where
     * @param string|array $field
     * @param array $with
     * @param array $order
     * @return array
     * @author yashuai<1762910894@qq.com>
     */
    public function getPageList(array $where = [], $field = ["*"], array $with = [], array $order = [])
    {
        $count = $this->count($where);
        $list  = [];
        if (!empty($count)) {
            [$page, $limit] = $this->getPageValue();
            $list = $this->selectList($where, $field, $page, $limit, $with, $order)->toArray();
        }
        return compact('list', 'count');
    }

    /**
     * @Desc 现有query分页数据
     * @param Query $query
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author yashuai<1762910894@qq.com>
     */
    public function queryGetPageList(Query $query)
    {
        [$page, $limit] = $this->getPageValue();
        $count = $query->count();
        $list  = [];
        if (!empty($count)) {
            $list = $query->page($page, $limit)->select()->toArray();
        }
        return compact('list', 'count');
    }

    /**
     * @Desc 空列表
     * @return array
     * @author yashuai<1762910894@qq.com>
     */
    public function notDataList()
    {
        return ['list' => [], 'count' => 0];
    }

    /**
     * @Desc 列表排序参数
     * @param array $params
     * @param array $field_list
     * @return array|string[]
     * @author yashuai<1762910894@qq.com>
     */
    public function listOrderParams(array $params = [], array $field_list = [])
    {
        $field = _array_field($params, 'OrderField');
        if (empty($field) || !in_array($field, $field_list)) return [];

        $type = _array_field($params, 'OrderType', 'ascending');
        $type = $type == 'ascending' ? 'asc' : 'desc';
        return [$field => $type];
    }

    /**
     * @Desc 获取分页
     * @return int[]
     * @author yashuai<1762910894@qq.com>
     */
    public function getPageValue()
    {
        $no_page = app()->request->param('NoPage', false);
        if (!empty($no_page)) {
            return [0, 0];
        }
        $page  = app()->request->param('Page', 1);
        $limit = app()->request->param('Size', 10);

        return [(int)$page, (int)$limit];
    }

    /**
     * @Desc 修改记录状态
     * @param $id
     * @param int $status
     * @param array|int[] $statusAll
     * @param string $status_field
     * @return mixed
     * @author yashuai<1762910894@qq.com>
     */
    public function updateStatus($id, int $status = 0, string $status_field = 'Status', array $statusAll = [1, 2])
    {
        if (!in_array($status, $statusAll)) {
            _throw('参数验证异常，状态不在范围');
        }
        return $this->update($id, [$status_field => $status]);
    }

    /**
     * @Desc 获取下拉框数据
     * @param array $where
     * @param string $label
     * @param string $value
     * @return array
     * @author yashuai<1762910894@qq.com>
     */
    public function select(array $where = [], string $label = '', string $value = '')
    {
        $result = $this->selectList($where, [$label, $value])->toArray();
        $select = [];
        foreach ($result as $val) {
            $select[] = [
                'label' => $val[$label],
                'value' => $val[$value]
            ];
        }
        return ['select' => $select];
    }

    /**
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public function __call($name, $arguments)
    {
        return call_user_func_array([$this->logic, $name], $arguments);
    }

}
